<template>
  <div class="course-list">
    <Header />
    <div class="category-nav">
      <div class="container">
        <div class="filter-group">
          <span class="label">课程分类：</span>
          <a 
            v-for="category in categories" 
            :key="category.id"
            :class="{ active: currentCategory === category.id }"
            @click="currentCategory = category.id"
            href="#">
            {{ category.name }}
          </a>
        </div>
        <div class="filter-group">
          <span class="label">排序方式：</span>
          <a 
            v-for="sort in sortOptions" 
            :key="sort.id"
            :class="{ active: currentSort === sort.id }"
            @click="currentSort = sort.id"
            href="#">
            {{ sort.name }}
          </a>
        </div>
        <div class="filter-group">
          <span class="label">价格区间：</span>
          <a 
            v-for="price in priceRanges" 
            :key="price.id"
            :class="{ active: currentPrice === price.id }"
            @click="currentPrice = price.id"
            href="#">
            {{ price.name }}
          </a>
        </div>
      </div>
    </div>
    <div class="container">
      <div class="course-grid">
        <div 
          v-for="course in courses" 
          :key="course.id" 
          class="course-card"
        >
          <div class="course-image" @click="goToCourseDetail(course.id)">
            <img :src="course.image" :alt="course.title">
          </div>
          <div class="course-info">
            <h3 class="course-title" @click="goToCourseDetail(course.id)">{{ course.title }}</h3>
            <p class="course-desc">{{ course.description }}</p>
            <div class="teacher-info">
              <img :src="course.teacher.avatar" :alt="course.teacher.name" class="teacher-avatar">
              <div class="teacher-detail">
                <span class="teacher-name">{{ course.teacher.name }}</span>
                <span class="teacher-title">{{ course.teacher.title }}</span>
              </div>
            </div>
            <div class="course-stats">
              <span class="student-count">
                <i class="fas fa-user"></i> {{ course.studentCount }}人学习
              </span>
              <span class="lesson-count">
                <i class="fas fa-book"></i> {{ course.lessonCount }}课时
              </span>
            </div>
            <div class="course-footer">
              <span class="price" v-if="course.price > 0">¥{{ course.price }}</span>
              <span class="price free" v-else>免费</span>
              <button class="buy-btn" @click.stop="addToCart(course)">立即购买</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import Header from '../components/Header.vue'

const router = useRouter()

const currentCategory = ref('all')
const currentSort = ref('default')
const currentPrice = ref('all')

const categories = [
  { id: 'all', name: '全部' },
  { id: 'python', name: 'Python' },
  { id: 'java', name: 'Java' },
  { id: 'web', name: 'Web前端' },
  { id: 'bigdata', name: '大数据' },
  { id: 'ai', name: '人工智能' }
]

const sortOptions = [
  { id: 'default', name: '默认排序' },
  { id: 'newest', name: '最新' },
  { id: 'hotest', name: '最热' },
  { id: 'price', name: '价格' }
]

const priceRanges = [
  { id: 'all', name: '全部' },
  { id: 'free', name: '免费' },
  { id: 'paid', name: '付费' }
]

const courses = [
  {
    id: 1,
    title: 'Python零基础入门到实战',
    description: '从零开始学习Python，掌握Python编程基础知识',
    image: 'https://img3m5.ddimg.cn/19/33/12100014055-1_h_1738829405.jpg',
    teacher: {
      name: '张老师',
      title: '高级工程师',
      avatar: 'https://img61.ddimg.cn/2024/3/26/2024032610360944729.jpg'
    },
    studentCount: 1234,
    lessonCount: 48,
    price: 299
  },
  {
    id: 2,
    title: 'Java高级开发工程师',
    description: '深入学习Java核心技术，掌握企业级开发技能',
    image: 'https://img3m1.ddimg.cn/64/27/12100020931-1_h_1739000101.jpg',
    teacher: {
      name: '李老师',
      title: '资深架构师',
      avatar: 'https://img61.ddimg.cn/2024/3/26/2024032610360944729.jpg'
    },
    studentCount: 2345,
    lessonCount: 56,
    price: 399
  },
  {
    id: 3,
    title: 'Web前端开发实战',
    description: '学习HTML5、CSS3、JavaScript等前端技术',
    image: 'https://img3m5.ddimg.cn/19/33/12100014055-1_h_1738829405.jpg',
    teacher: {
      name: '王老师',
      title: '前端专家',
      avatar: 'https://img61.ddimg.cn/2024/3/26/2024032610360944729.jpg'
    },
    studentCount: 3456,
    lessonCount: 42,
    price: 0
  }
]

const goToCourseDetail = (courseId) => {
  router.push(`/course/${courseId}`)
}

const addToCart = (course) => {
  // 这里可以添加将课程添加到购物车的逻辑
  router.push('/cart')
}
</script>

<style scoped>
.course-list {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-top: 64px;
}

.category-nav {
  background: white;
  padding: 20px 0;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.filter-group {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.filter-group:last-child {
  margin-bottom: 0;
}

.label {
  color: #666;
  margin-right: 20px;
  font-size: 14px;
  min-width: 70px;
}

.filter-group a {
  color: #333;
  text-decoration: none;
  margin-right: 20px;
  font-size: 14px;
  padding: 4px 12px;
  border-radius: 16px;
  transition: all 0.3s;
}

.filter-group a:hover,
.filter-group a.active {
  color: #ff6b00;
  background: #fff2e8;
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-top: 20px;
}

.course-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.course-image {
  width: 100%;
  height: 160px;
  overflow: hidden;
}

.course-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.course-info {
  padding: 15px;
}

.course-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 8px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.5;
}

.teacher-info {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.teacher-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 8px;
}

.teacher-detail {
  display: flex;
  flex-direction: column;
}

.teacher-name {
  font-size: 14px;
  color: #333;
}

.teacher-title {
  font-size: 12px;
  color: #999;
}

.course-stats {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  font-size: 12px;
  color: #666;
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 18px;
  color: #ff6b00;
  font-weight: bold;
}

.price.free {
  color: #52c41a;
}

.buy-btn {
  padding: 6px 16px;
  background: #ff6b00;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.buy-btn:hover {
  background: #ff8533;
}

@media screen and (max-width: 1200px) {
  .course-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media screen and (max-width: 992px) {
  .course-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media screen and (max-width: 768px) {
  .course-grid {
    grid-template-columns: repeat(1, 1fr);
  }
}
</style> 